//
//  WCTimerManager.m
//  WC
//
//  Created by 杨鑫 on 15/4/30.
//  Copyright (c) 2015年 智康. All rights reserved.
//

#import "WCTimer.h"
//#import "he"

@import UIKit;

@interface WCTimer ()

@property (strong, nonatomic) NSTimer *timer;

@end

@implementation WCTimer
+(int)getRandomNumber:(int)from to:(int)to

{
    
    return (int)(from + (arc4random() % (to -from + 1)));
    
}
- (void)start:(NSTimeInterval)seconds
{
    _timer = [NSTimer scheduledTimerWithTimeInterval:seconds
                                              target:self
                                            selector:@selector(countDownMethod)
                                            userInfo:nil
                                             repeats:YES];
}
- (void)deallocTimer{
    [_timer invalidate];

}
- (void)countDownMethod
{
    _count--;
    if (_count > 0) {
        //
        if (_securityCode) {
            [_securityCode setTitle:[NSString stringWithFormat:@"%ld秒", (long)_count] forState:UIControlStateNormal];
            _securityCode.enabled = NO;
//            [_securityCode setTitle:[NSString stringWithFormat:@"%ld秒", (long)_count] forState:UIControlStateNormal | UIControlStateDisabled];
        }
        
        if (_timing)
            _timing(_count);
    } else {
           _securityCode.enabled = YES;
        [_timer invalidate];
        _timer = nil;
     
        //
        if (_securityCode) {
            _securityCode.enabled = YES;
            _securityCode.backgroundColor = HexRGB(0x4FB3D7);
            [_securityCode setTitle:@"发送验证码" forState:UIControlStateNormal];
        }
        
        if(_timeEnd)
            _timeEnd();
    }
}

- (void)setSecurityCode:(UIButton *)securityCode
{
    _securityCode = securityCode;
    
    securityCode.enabled = NO;
    securityCode.backgroundColor = [UIColor lightGrayColor];
//    NSString *str =[NSString stringWithFormat:@"%ld秒", (long)_count];
//    [securityCode setTitle:str forState:UIControlStateNormal | UIControlStateDisabled];
//    [securityCode setTitle:@"zhdfadfsd" forState:UIControlStateNormal];
    [securityCode setTitle:[NSString stringWithFormat:@"%ld秒", (long)_count] forState:UIControlStateNormal];
}

@end
